package com.game;


import java.util.Vector;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.Log;

public class BallManager {
	public Vector<ColorBall> balls;
	private int BaseBallSpeed;
	Context mContext;
	BackgroundMirror mBackgroundMirror;
	Vector<MirrorBall> mMirrorBalls;
	
	private int stopCounter;
	private int stopTime;
	private boolean stopActive;
	

	

	
	
	public BallManager(Context context, int numbBalls, BackgroundMirror bgmirror, Vector<MirrorBall> mbref)
	{
		mContext = context;
		mBackgroundMirror = bgmirror;
		mMirrorBalls = mbref;
		
		balls = new Vector<ColorBall>();
		CreateBalls(numbBalls,3,4);
		setBaseBallSpeed(2);
		
		stopCounter = 0;
		stopTime = 300;
		stopActive = false;
	}
	
	public void CreateBalls(int numbBalls, int minSpeed, int maxSpeed)
	{
		Point auxPoint, auxHeading;
		ColorBall auxBall;
		Random generator = new Random();
		int imgRef = 0;
		for(int i = 0; i < numbBalls; i++)
		{
			auxPoint = new Point();
			auxHeading = new Point();
			auxPoint.x = generator.nextInt(200)+10;
			auxPoint.y = generator.nextInt(300)+10;
			
			auxHeading.x = auxPoint.x;
			auxHeading.y = auxPoint.y;
			
			switch (generator.nextInt(4)) {
			case 0: imgRef = R.drawable.ballblue;	break;
			case 1: imgRef = R.drawable.ballgreen;	break;
			case 2: imgRef = R.drawable.ballred;	break;
			case 3: imgRef = R.drawable.ballyellow;	break;
			default:
				break;
			}
			
			int speedMargin = maxSpeed-minSpeed;
			if(speedMargin <= 0) speedMargin = 1;
			int speed = generator.nextInt(speedMargin)+minSpeed;
			if(speed <= 1) speed = 1;
			
			auxBall = new ColorBall(mContext, imgRef, auxPoint, mBackgroundMirror, mMirrorBalls,speed,i);
			auxBall.setHeading(auxHeading);
			
			balls.add(auxBall);			
		}		
	}
	
	public void onDraw(Canvas canvas)
	{
		for (ColorBall ball : balls) {
           ball.onDraw(canvas);
          }
	}
	
	public void Update()
	{
		for(ColorBall ball : balls)
		{
			ball.Update();
		}
		if(stopActive)
		{
			stopCounter++;
			if(stopCounter >= stopTime) resumeBalls();
		}
	}

	public void CleanUp() {
		balls.clear();		
	}

	public void setBaseBallSpeed(int baseBallSpeed) {
		BaseBallSpeed = baseBallSpeed;
	}

	public int getBaseBallSpeed() {
		return BaseBallSpeed;
	}

	
	
	public boolean AllBallsDestroyed() 
	{ 
		boolean allDestroyed = true;
		
		for(ColorBall ball: balls)
		{
			if(ball.getExploded() == false)
			{
				allDestroyed = false;
			}
		}
		
		
		return allDestroyed;
	}
	
	public void stopBalls()
	{
		stopActive = true;
		stopCounter = 0;
		for(ColorBall ball : balls)
		{
			ball.stopBall();
		}
	}
	
	public void resumeBalls()
	{
		Log.i("Gaisma::", "CALLING RESUME BALL");
		stopCounter = 0;
		for(ColorBall ball : balls)
		{
			ball.resumeBall();
		}
		stopActive = false;
	}
	
}
